package spcpy

import (
	"gitee.com/ymofen/gobase"
	"gitee.com/ymofen/golog"
	"github.com/spf13/cobra"
	"time"
)

func init() {
	gobase.ArgFuncRegister("subprobe", gobase.ArgFuncOption{Catalog: "corbaRoot", Func: func(args string, tag interface{}) (val interface{}, err error) {
		var cbrunner CorbaRunner
		var addCorbaTask = &cobra.Command{
			Use: "subprobe",
			Run: func(cmd *cobra.Command, args []string) {
				golog.Infof("subConf:%s", cbrunner.SubConf)
				golog.Infof("hex:%d", cbrunner.HexFlag)
				cbrunner.Run()
				gobase.HookExitSignalAndInterval(0, time.Second*10, func() {

				})
			},
		}
		addCorbaTask.Flags().StringVarP(&cbrunner.SubConf, "subConf", "i", "conntype=redis&connstr=", "")
		addCorbaTask.Flags().IntVarP(&cbrunner.HexFlag, "hex", "", 0, "hex=0")
		return addCorbaTask, nil
	}})

}

type CorbaRunner struct {
	Id      string
	SubConf string
	HexFlag int
	sp      SubProbe
}

func (this *CorbaRunner) Run() error {
	if len(this.Id) == 0 {
		this.Id = "SubProbe"
	}
	this.sp.Init()
	this.sp.hexFlag = int8(this.HexFlag)
	this.sp.UpdateConf(this.SubConf)
	return nil
}
